Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- MFarm: Start the farming process
- -- Function to load the setup data
- function loadSetup()
- if not fs.exists("farmSetup") then
- print("Setup not found. Please run MFarmSetup first.")
- return nil, nil, nil, nil, nil
- end
- local file = fs.open("farmSetup", "r")
- local seedItem = file.readLine()
- local fuelItem = file.readLine()
- local fullyGrownItem = file.readLine()
- local farmSize = tonumber(file.readLine())
- local sleepTime = tonumber(file.readLine())
- file.close()
- return seedItem, fuelItem, fullyGrownItem, farmSize, sleepTime
- end
- -- Function to check and refuel the turtle
- function checkAndRefuel(fuelItem)
- if turtle.getFuelLevel() < 500 then
- for slot = 1, 16 do
- turtle.select(slot)
- local item = turtle.getItemDetail()
- if item and item.name == fuelItem then
- turtle.refuel()
- if turtle.getFuelLevel() >= 500 then
- break
- end
- end
- end
- if turtle.getFuelLevel() < 500 then
- print("Turtle needs more fuel. Setup failed.")
- return false
- end
- end
- return true
- end
- -- Function to sort items from turtle inventory and chest
- function sortItems(seedItem, fuelItem, fullyGrownItem)
- local hasSeed = false
- local hasFuel = false
- local hasFullyGrown = false
- -- Check turtle inventory
- for slot = 1, 16 do
- turtle.select(slot)
- local item = turtle.getItemDetail()
- if item then
- if item.name == seedItem then
- turtle.transferTo(1)
- hasSeed = true
- elseif item.name == fuelItem then
- turtle.transferTo(2)
- hasFuel = true
- elseif item.name == fullyGrownItem then
- turtle.transferTo(3)
- hasFullyGrown = true
- end
- end
- end
- -- Check chest below
- if not hasSeed or not hasFuel or not hasFullyGrown then
- for slot = 1, 16 do
- turtle.select(slot)
- if not hasSeed then
- turtle.suckDown()
- local item = turtle.getItemDetail()
- if item and item.name == seedItem then
- turtle.transferTo(1)
- hasSeed = true
- else
- turtle.dropDown()
- end
- end
- if not hasFuel then
- turtle.suckDown()
- local item = turtle.getItemDetail()
- if item and item.name == fuelItem then
- turtle.transferTo(2)
- hasFuel = true
- else
- turtle.dropDown()
- end
- end
- if not hasFullyGrown then
- turtle.suckDown()
- local item = turtle.getItemDetail()
- if item and item.name == fullyGrownItem then
- turtle.transferTo(3)
- hasFullyGrown = true
- else
- turtle.dropDown()
- end
- end
- end
- end
- return hasSeed, hasFuel, hasFullyGrown
- end
- -- Function to return items to the chest
- function returnItems()
- for slot = 4, 16 do
- turtle.select(slot)
- turtle.dropDown()
- end
- end
- -- Function to farm crops
- function farm(size)
- turtle.select(1) -- Ensure seeds are selected
- turtle.forward() -- Move forward to start farming
- for i = 1, size do
- for j = 1, size do
- if turtle.detectDown() then
- turtle.digDown()
- end
- turtle.placeDown()
- if j < size then
- turtle.forward()
- end
- end
- if i < size then
- if i % 2 == 1 then
- turtle.turnRight()
- turtle.forward()
- turtle.turnRight()
- else
- turtle.turnLeft()
- turtle.forward()
- turtle.turnLeft()
- end
- end
- end
- -- Return to the starting position
- turtle.forward()
- turtle.turnLeft()
- for k = 1, size do
- turtle.forward()
- end
- turtle.turnLeft()
- for k = 1, size + 1 do
- turtle.forward()
- end
- turtle.turnLeft()
- turtle.forward()
- turtle.turnLeft()
- end
- -- Function to check fuel level and refuel if necessary
- function manageFuel(fuelItem)
- if turtle.getFuelLevel() < 500 then
- print("Refueling...")
- checkAndRefuel(fuelItem)
- end
- if turtle.getFuelLevel() < 500 then
- print("Turtle needs more fuel. Setup failed.")
- return false
- end
- return true
- end
- -- Function to manage the farming cycle
- function farmingCycle(seedItem, fuelItem, fullyGrownItem, farmSize, sleepTime)
- while true do
- farm(farmSize)
- -- Dump items from slots 4-16 into the chest below
- returnItems()
- -- Check and manage fuel
- if not manageFuel(fuelItem) then
- print("Turtle needs more fuel. Pausing for " .. sleepTime .. " seconds.")
- os.sleep(sleepTime) -- Wait for the specified sleep time
- print("Retrying farming process.")
- else
- print("Fuel level sufficient. Pausing for " .. sleepTime .. " seconds.")
- os.sleep(sleepTime) -- Wait for the specified sleep time
- end
- end
- end
- -- Main function
- function main()
- local seedItem, fuelItem, fullyGrownItem, farmSize, sleepTime = loadSetup()
- if not seedItem then
- return
- end
- -- Confirm setup information
- print("Setup Information:")
- print("Seed: " .. seedItem)
- print("Fuel: " .. fuelItem)
- print("Fully Grown Crop: " .. fullyGrownItem)
- print("Farm Size: " .. farmSize .. "x" .. farmSize)
- print("Sleep Time: " .. sleepTime .. " seconds")
- print("")
- print("Is this information correct? (yes/no)")
- local confirmation = string.lower(read())
- if confirmation == "yes" then
- if checkAndRefuel(fuelItem) then
- print("Checking for required items in inventory and chest...")
- local hasSeed, hasFuel, hasFullyGrown = sortItems(seedItem, fuelItem, fullyGrownItem)
- if hasSeed and hasFuel and hasFullyGrown then
- print("All required items are present and sorted into the correct slots.")
- print("Ensuring seeds are in the first slot...")
- turtle.select(1)
- print("Setup confirmed.")
- print("Please manually place the seeds in the first slot of the turtle's inventory.")
- print("Press any key to continue once this is done.")
- os.pullEvent("key")
- print("Ready to start farming.")
- farmingCycle(seedItem, fuelItem, fullyGrownItem, farmSize, sleepTime) -- Start the farming cycle
- else
- print("Required items are missing. Setup failed.")
- returnItems()
- end
- else
- print("Setup failed. Please provide more fuel.")
- end
- else
- print("Setup information incorrect. Please run MFarmSetup again.")
- end
- end
- -- Start the farming process
- main()
Add Comment
Please, Sign In to add comment